/*
 * 文件名：LogSchedule.java	 
 * 时     间：下午6:31:34
 * 作     者：lyf       
 * 版     权： 2012-2022 湖南智软网络科技, 公司保留所有权利.
 * 联     系：http://www.073586.com/
 */
package com.schedule.log;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import com.cz073586.tools.util.Log4jUtil;
import com.local.model.LocalTimer;
import com.local.service.face.CarInAndOutService;
import com.local.service.face.LocalTimerService;
import com.qianhai.common.utils.Tools;

/**
 * 
 * 类描述：系统定时器处理，自动清除进出场以及进出副本表数据
 * @ClassName: CarInAndOutSchedule 
 * @author Zhangw
 * @date 2016年3月14日 上午11:14:03
 */
@Service
@EnableScheduling
public class CarInAndOutSchedule
{
	
	private Log4jUtil log = Log4jUtil.getLog4j(CarInAndOutSchedule.class);
	
	@Autowired
	protected CarInAndOutService carInAndOutService;
	
	@Autowired
	private LocalTimerService localTimerService;
	
	/**
	 * 
	 * 方法描述：每隔一天检测，清理进出场以及进出副本表数据 
	 * @Title: clearFixedLog 
	 * @param 
	 * @return void
	 * @user Administrator
	 * 2016年3月14日
	 * @throws
	 */
	@Scheduled(fixedDelay = 1000 * 60 * 60 * 24 )
	public void clearFixedLog()
	{
		Date nowDate=new Date();
		try
		{
			LocalTimer lt=localTimerService.loadLocalTimerInfo();
			if(!Tools.isEmpty(lt)&&lt.getAttr().equalsIgnoreCase("0")){
				if(!Tools.isEmpty(lt.getCarInDays())){
					Long time=Long.parseLong(lt.getCarInDays())*86400000;
					Date nowTime=new Date(nowDate.getTime()-time);
					carInAndOutService.deleteCarParkIn(nowTime);
				}
				if(!Tools.isEmpty(lt.getCarOutDays())){
					Long time=Long.parseLong(lt.getCarOutDays())*86400000;
					Date nowTime=new Date(nowDate.getTime()-time);
					carInAndOutService.deleteCarParkOut(nowTime);
				}
				if(!Tools.isEmpty(lt.getCarInHistoryDays())){
					Long time=Long.parseLong(lt.getCarInHistoryDays())*86400000;
					Date nowTime=new Date(nowDate.getTime()-time);
					carInAndOutService.deleteCarParkInCopy(nowTime);
				}
				if(!Tools.isEmpty(lt.getCarOutHistoryDays())){
					Long time=Long.parseLong(lt.getCarOutHistoryDays())*86400000;
					Date nowTime=new Date(nowDate.getTime()-time);
					carInAndOutService.deleteCarParkOutCopy(nowTime);
				}
			}
			System.out.println(nowDate + "   执行定时器清除数据!");
		} catch (Throwable e)
		{
			log.error("", e);
		}
	}
	
	/**
	 * 
	 * 方法描述：每隔1分钟批量新增进场索引
	 * @Title: autoBatchAddCarParkInIndexLog 
	 * @param 
	 * @return void
	 * @user lt
	 * 2016年3月18日
	 * @throws
	 */
	@Scheduled(fixedDelay = 1000 * 60 *1 )
	public void autoBatchAddCarParkInIndexLog()
	{
		Date nowDate=new Date();
		try
		{
			carInAndOutService.batchCreateCarParkInIndex();
			if(log.isDebugEnabled())
			{
			  log.debug("->"+nowDate+",执行定时器批量新增索引");
			}
		} catch (Throwable e)
		{
			log.error("", e);
		}
	}
	
	/**
	 * 
	 * 方法描述：删除进场表与进场结果表的。冗余数据
	 * @Title: clearRepertory 
	 * @param 
	 * @return void
	 * @user Administrator
	 * 2016年6月1日
	 * @throws
	 */
	@Scheduled(fixedDelay = 1000 * 60 * 60 * 24 )
	public void clearRepertory()
	{
		Date nowDate=new Date();
		try
		{
			//删除进场表与进场结果表的。冗余数据
			carInAndOutService.deleteCarInAndRepertory();
			if(log.isDebugEnabled())
			{
			  log.debug("->"+nowDate+",执行定时器批量新增索引");
			}
		} catch (Throwable e)
		{
			log.error("", e);
		}
	}
}
